#include "stdio.h"
#include "stdlib.h"
//nvcc cuda/2VectorAddition2.cu -o cmake-build/cuda/2VectorAddition2 && ./cmake-build/cuda/2VectorAddition2
__global__ void add(int*a,int*b,int*c)
{
    c[blockIdx.x]=a[blockIdx.x]+b[blockIdx.x];
}
void randomInts(int*a,int n)
{
    for(int i=0;i<n;++i)
        a[i]=i;
}
#define N 512
int main(void)
{
    int *a,*b,*c;
    int *d_a,*d_b,*d_c;
    int size=sizeof(int)*N;
    
    cudaMalloc((void**)&d_a,size);
    cudaMalloc((void**)&d_b,size);
    cudaMalloc((void**)&d_c,size);
    
    a=(int*)malloc(size);
    b=(int*)malloc(size);
    c=(int*)malloc(size);
    randomInts(a,N);
    randomInts(b,N);
    
    cudaMemcpy(d_a,a,size,cudaMemcpyHostToDevice);
    cudaMemcpy(d_b,b,size,cudaMemcpyHostToDevice);
    add<<<N,1>>>(d_a,d_b,d_c);
    cudaMemcpy(c,d_c,size,cudaMemcpyDeviceToHost);
    
    cudaFree(d_a);
    cudaFree(d_b);
    cudaFree(d_c);
    
    for(int i=0;i<N;++i)
        printf("%3d+%3d=%4d\n",a[i],b[i],c[i]);
    
    free(a);
    free(b);
    free(c);
    return 0;
}

